package vistas;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.table.DefaultTableModel;

import clases.Horarios_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import javax.swing.ListSelectionModel;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import java.awt.Cursor;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Horarios_buscarform extends JInternalFrame {
	public static JTextField textField;
	public static JTable table;
	private int filas;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Horarios_buscarform frame = new Horarios_buscarform(0);
			frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 */
	public Horarios_buscarform(int tipo) {
		setFrameIcon(new ImageIcon(Asignarhorarios_buscarform.class.getResource("/icons/calendar.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Horario");
		filas=0;
		setBounds(100, 100, 674, 450);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("Nombres");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label.setBounds(10, 13, 59, 16);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField.setColumns(10);
		textField.setBounds(79, 11, 451, 20);
		getContentPane().add(textField);
		
		JButton button = new JButton("Buscar");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.setContentAreaFilled(false);
		button.setMnemonic(KeyEvent.VK_ENTER);
		button.setIcon(new ImageIcon(Horarios_buscarform.class.getResource("/icons/buscar.png")));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodoBuscar();
			}
		});
		button.setBounds(540, 11, 108, 23);
		getContentPane().add(button);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 40, 638, 361);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"CODIGO", "Nombre", "Hora Inicio", "Hora Fin"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				true, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(0);
		table.getColumnModel().getColumn(0).setMinWidth(0);
		table.getColumnModel().getColumn(0).setMaxWidth(0);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmModificar = new JMenuItem("Modificar");
		mntmModificar.setIcon(new ImageIcon(Horarios_buscarform.class.getResource("/icons/modificar_pq.png")));
		mntmModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Horarios_ingresarform modificahorario = new Horarios_ingresarform(table.getValueAt(table.getSelectedRow(), 0).toString());
				Principal.contenedor.add(modificahorario);
				modificahorario.setVisible(true);
			}
		});
		popupMenu.add(mntmModificar);
		
		if(tipo==1)
		{
			JMenuItem mntmSeleccionar_1 = new JMenuItem("Seleccionar");
			mntmSeleccionar_1.setIcon(new ImageIcon(Horarios_buscarform.class.getResource("/icons/seleccionar_pq.png")));
			mntmSeleccionar_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Asignarhorarios_ingresarform.codigohorario=table.getValueAt(table.getSelectedRow(), 0).toString();
					Asignarhorarios_ingresarform.thorario.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
					dispose();
				}
			});
			popupMenu.add(mntmSeleccionar_1);
		}
		metodoBuscar();
}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		
		try
		{
			filas=0;
			DefaultTableModel mhorario = (DefaultTableModel)table.getModel();
			while(mhorario.getRowCount()>0) mhorario.removeRow(0);
			ResultSet resultado = new Horarios_class().mostrar();
			while(resultado.next())
			{
				mhorario.addRow(new Object[filas]);
				mhorario.setValueAt(resultado.getString("codigohorario"), filas, 0);
				mhorario.setValueAt(resultado.getString("nombre"), filas, 1);
				mhorario.setValueAt(resultado.getString("horainicio"), filas, 2);
				mhorario.setValueAt(resultado.getString("horafin"), filas, 3);
				filas++;
			}
			resultado.close();
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, ex.toString());
		}
		
	
	}
}
